package cn.hznc.controller.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.controller.service.QuerySqlServerService;
import cn.hznc.controller.service.order.AbutmentService;
import cn.hznc.controller.service.order.OrderManageService;
import cn.hznc.controller.service.sharefile.ShareFileService;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.order.AbutmentOrderReq;
import cn.hznc.mapper.ConnectMaterialNosMappper;
import cn.hznc.mapper.OrderMapper;
import cn.hznc.mapper.ProductionMapper;
import cn.hznc.utils.CommonUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：Administrator
 * @Date：2023/9/25 13:23
 */
@Component
@Slf4j
public class OrderTask{

    @Autowired
    private OrderManageService orderManageService;

    @Autowired
    private ShareFileService shareFileService;

    @Autowired
    private QuerySqlServerService querySqlServerService;
    @Resource
    private OrderMapper  orderMapper;

    @Resource
    private AbutmentService abutmentService;

    @Resource
    private ConnectMaterialNosMappper connectMaterialNosMappper;

    @Resource
    private ProductionMapper productionMapper;


    @Scheduled(cron ="0 0 0 * * ? ")
    public void abutmentOrderStatus(){
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        wrapper.ne("abutment_status" , 9);
        List<OrderEntity> orderEntities = orderMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(orderEntities)){
            orderManageService.abutmentOrderStatus(orderEntities.stream().map(OrderEntity::getTaskId).collect(Collectors.toList()));
        }
    }

    @Scheduled(cron ="0 20 20 * * ? ")
    public void autoAbutmentOrder() throws ParseException {
        AbutmentOrderReq abutmentOrderReq = new AbutmentOrderReq();
        Map<String, String> dateMap = CommonUtil.minusTime24h();
        abutmentOrderReq.setStartTime(CommonUtil.parseDateReload(dateMap.get("Data2")));
        abutmentOrderReq.setEndTime(CommonUtil.parseDateReload(dateMap.get("Data1")));
        abutmentOrderReq.setMaterialNos(Arrays.asList("434" , "435" , "437"));
        //存储此次快照的物料编码
        ConnectMaterialNosEntity connectMaterialNosEntity = new ConnectMaterialNosEntity();
        connectMaterialNosEntity.setMaterialnos(JSONArray.toJSONString(abutmentService.abutmentData(abutmentOrderReq)));
        connectMaterialNosEntity.setCreateTime(new Date());
        connectMaterialNosMappper.insert(connectMaterialNosEntity);
    }

    @Scheduled(cron ="0 35 20 * * ? ")
    public void autoAbutmentDataConnect() throws ParseException {
        //关联快照的物料编码
        QueryWrapper<ConnectMaterialNosEntity> queryWrapper = new QueryWrapper<>();
        Map<String, String> startAndEndOfOneDay = CommonUtil.findStartAndEndOfOneDay(CommonUtil.transferDate(new Date()));
        queryWrapper.between( "create_time" , startAndEndOfOneDay.get("startTime") , startAndEndOfOneDay.get("endTime"));
        List<ConnectMaterialNosEntity> connectMaterialNosEntities = connectMaterialNosMappper.selectList(queryWrapper);
        List<String> connectMaterialNos = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(connectMaterialNosEntities) && StringUtils.isNotBlank(connectMaterialNosEntities.get(0).getMaterialnos())){
            connectMaterialNos = JSONArray.parseArray(connectMaterialNosEntities.get(0).getMaterialnos() , String.class);
        }
        QueryWrapper<ProductionEntity> wrapper = new QueryWrapper<>();
        wrapper.in("material_no", connectMaterialNos);
        ProductionEntity productionEntity = productionMapper.selectOne(wrapper);
        abutmentService.dataConnect(Arrays.asList(productionEntity));
    }


    @Scheduled(cron ="0 0 22 * * ? ")
    public void insertShareFileAtRegularTime(){
        shareFileService.insertShareFile();
    }



}
